package com.itheima.securityexception;

import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 林道样
 * @date 2020-03-05 16:59
 */
public class MyException implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Result result = new Result(false, MessageConstant.INSUFFICIENT_AUTHORITY);
        httpServletResponse.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSON(result).toString();
        httpServletResponse.getWriter().print(json);
    }
}
